import { login } from "./api/login.js";

// 所需要用的dom元素
const doms = {
    formContainer: document.querySelector("#formContainer"),
    userName: document.querySelector("#userName"),
    userPassword: document.querySelector('#userPassword'),
    btnSubmit: document.querySelector("#btnSubmit")
}
let isLoginning = false;

document.querySelector("#line").addEventListener('animationend', (e) => {
    e.target.classList.remove("animate__fadeInRightBig");
    // animate__lightSpeedOutRight animate__infinite animate__slow
    e.target.classList.add("animate__zoomOutUp");
    e.target.classList.add("animate__infinite");
    e.target.classList.add("animate__slow");
    e.target.classList.add("animate__delay-1s");
});

doms.formContainer.onsubmit = async function (e) {
    e.preventDefault();
    if (isLoginning) {
        return; // 登陆中
    }
    const userName = doms.userName.value;
    const userPwd = doms.userPassword.value;

    if (!userName) {
        alert("请输入帐号");
        return;
    }
    if (!userPwd) {
        alert("请输入密码");
        return;
    }

    isLoginning = true;
    doms.btnSubmit.value = "登陆中...";

    const resp = await login({
        userName,
        userPwd
    });
    console.log(resp);
    if (resp.code === 200) {
        alert("登陆成功");
        location.href = "./index.html";
    } else {
        alert("登陆失败");
    }
    isLoginning = false;
    doms.btnSubmit.value = "登陆";
    return;
}